import 'dart:async';
import 'dart:math';

import 'package:chewie/chewie.dart';
import 'package:fijkplayer/fijkplayer.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:video_player/video_player.dart';

class VideoView extends StatefulWidget {
  final String url;
  final String? cover;
  final bool autoPlay;
  final bool looping;
  final bool autoFull;
  final double aspectRatio;
  final double? widths;
  final double? heights;
  VideoView(this.url,
      {Key? key,
      this.cover,
      this.autoFull = false,
      this.autoPlay = true,
      this.looping = false,
      this.aspectRatio = 16 / 9,
      this.widths,
      this.heights})
      : super(key: key);

  @override
  _VideoViewState createState() => _VideoViewState();
}

class _VideoViewState extends State<VideoView> {
  late VideoPlayerController _videoPlayerController;
  late ChewieController _chewieController;

  @override
  void initState() {
    super.initState();

    _videoPlayerController = VideoPlayerController.network(widget.url);
    _chewieController = ChewieController(
      videoPlayerController: _videoPlayerController,
      aspectRatio: widget.aspectRatio,
      autoPlay: widget.autoPlay,
      looping: widget.looping,
      allowFullScreen: widget.autoFull,
    );
//
  }

  @override
  void dispose() {
    super.dispose();
    _videoPlayerController.dispose();
    _chewieController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return AspectRatio(
      aspectRatio: 16 / 9,
      child: Container(
        width: widget.widths,
        height: widget.heights,
        child: Chewie(
          controller: _chewieController,
        ),
      ),
    );
  }
}
